/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.examsys.view;

import com.iss.examsys.ejb.ExamSessionBean;
import com.iss.examsys.entity.ExamSession;
import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Named;

/**
 *
 * @author Quek Ting Hong
 */
@ManagedBean
public class ExamSessionConverter implements Converter{

    @EJB ExamSessionBean examSessionBean;
    /**
     * Creates a new instance of ExamSessionConverter
     */
    public ExamSessionConverter() {
    }
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null) {
                    return null;
            }
            try{
                long id = Long.parseLong(value);
                return examSessionBean.findById(id);
            }
            catch(Exception e){
                e.printStackTrace();
                return null;
            }
            
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
            if (value == null) {
                    return null;
            }
            Long id = ((ExamSession) value).getId();

            return (id != null) ? id.toString() : null;
    }
}
